package com.wimcorp.magic.host;

import java.util.Collection;

import com.wimcorp.magic.behaviour.Flying;
import com.wimcorp.magic.behaviour.Intimidate;
import com.wimcorp.magic.behaviour.Islandwalk;
import com.wimcorp.magic.behaviour.Reach;
import com.wimcorp.magic.behaviour.Tapped;
import com.wimcorp.magic.behaviour.Unblockable;
import com.wimcorp.magic.board.CardType;
import com.wimcorp.magic.cards.PlayingCard;
import com.wimcorp.magic.color.Color;

public class BlockValidator {

	private PlayingCard defender;
	private PlayingCard attacker;

	public BlockValidator(PlayingCard defender, PlayingCard attacker) {
		this.defender = defender;
		this.attacker = attacker;
	}

	public boolean canBlock() {
		if(defender.hasBehaviour(Tapped.instance())){
			return false;
		}
		if (attacker.hasBehaviour(Unblockable.instance())) {
			return false;
		}
		if (attacker.hasBehaviour(Flying.instance())) {
			if ((!defender.hasBehaviour(Flying.instance())) && (!defender.hasBehaviour(Reach.instance()))) {
				return false;
			}
		}
		if (attacker.hasBehaviour(Intimidate.instance())) {
			boolean canBlockInitimidate = false;
			for (Color color : attacker.getColors()) {
				if (defender.getColors().contains(color)) {
					canBlockInitimidate = true;
				}
			}
			if (!canBlockInitimidate) {
				return false;
			}
		}
		if (attacker.hasBehaviour(Islandwalk.instance())) {
			Collection<PlayingCard> lands = defender.getSummoningPlains().getCards(CardType.LAND);
			for(PlayingCard land : lands){
				if("Island".equals(land.getName())){
					return false;
				}
			}
		}
		
		return true;
	}

}
